0 value pcirepeat 3 value vcirepeat 0 value therepeat
0 value pcilast
: chc " config-l@" $find if drop else cr ." select a pci device" abort then ;
: $ex $find if execute else 2drop then ;
: cl! " config-l!" $ex ; : cl@ " config-l@" $ex ;
: cw@ " config-w@" $ex ; : cb@ " config-b@" $ex ;
: pc@ { adr } adr cl@ dup to pcilast
therepeat 0 ?do to pcilast adr cl@ loop ; : pciconfig
8 << swap b << rot 10 << + + ; : dumpbar { adr ; blo bhi }
adr pc@ -> blo blo if 25 spaces adr 8 u.r ." :"
blo 7 and 4 = if adr 4+ pc@ -> bhi bhi 8 u.r then
blo 8 u.r ." ." blo 7 and 4 = if -1 adr 4+ cl! adr 4+ pc@
bhi adr 4+ cl! 8 u.r then -1 adr cl! adr pc@ blo adr cl!
8 u.r cr then ;
: .configl ['] pc@ catch if drop ." ........" else lbflip 8 u.r then 4 ;
: .configw ['] cw@ catch if drop ." ...." else wbflip 4 u.r then 2 ;
: .configb ['] cb@ catch if drop ." .." else 2 u.r then 1 ;
defer .config ' .configl to .config
: lspcidump { bus dev fun ; adr }
bus 2 u.r ." :" dev 2 u.r ." ." fun 1 u.r
bus dev fun pciconfig -> adr adr c + pc@ dup 800000 and 0=
fun 0= and if 8 else 1 then swap 7f0000 and if 38 14 10 3
." -[" adr 18 + pc@ dup 10 >> ff and swap 8 >> ff and
2dup 2 u.r <> if ." -" 2 u.r 1 else drop 4 then ." ]" else
30 24 20 1c 18 14 10 7 9 then spaces ." [" adr pc@ dup
4 u.r ." :" 10 >> 4 u.r ." ] [" adr 8 + pc@ 8 >>
6 u.r ." ] " 100 0 do adr i + .config +loop cr 0 do adr +
dumpbar loop ; : lspcifun { bus dev fun ; adr } chc
bus dev fun pciconfig -> adr adr ['] pc@ catch if drop 0
else pcilast over = swap -1 <> and then if
bus dev fun lspcidump else 1 fun 0= if drop 8 then then ;
: lspcidev 8 0 do 2dup i lspcifun +loop 2drop ;
: lspcibus 20 0 do dup i lspcidev loop drop ;
: lspci pcirepeat to therepeat cr 100 0 do i lspcibus loop ;
: lsvci vcirepeat to therepeat cr 100 0 do i lspcibus loop ;
: setpci { bus dev fun reg ; adr }
chc bus dev fun pciconfig reg + -> adr
bus 2 u.r ." :" dev 2 u.r ." ." fun 1 u.r ."  = " adr 8 u.r adr cl@ ."  -> " 8 u.r
;

